package gosnoopers.fr;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

/*import common.GPS;*/

public class Maps extends FragmentActivity {

	public static LatLng POINT_1;
	private GoogleMap gMap;
	private List<Address> addresses;
	private Marker point_1;
	double lat = 0;
	double lng = 0;
	
	private Geocoder gcd;
	/*GPS gps;*/

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.maps);
		header();
		TextView text_view_landing_page_header = (TextView) findViewById(R.id.text_view_landing_page_header);
        text_view_landing_page_header.setText("Map detail");
		
		init();
		setValueForMap();
	}

	public void init() {
		/*		
		gps = new GPS(getApplicationContext());
		if(gps.canGetLocation()) {
			lat = gps.getLatitude();
			lng = gps.getLongitude();
		} else {
			gps.showSettingsAlert();
		}
		*/
		
		
        Intent iin= getIntent();
        Bundle b = iin.getExtras();
        
        if(b!=null)
        {
        	
        	lat =b.getDouble("LAT");
        	lng = b.getDouble("LONG");
            
        }
        
        System.out.println("FFFFFFFFFFFFFFFFFFFF "+lat);
    	  System.out.println("FFFFFFFFFFFFFFFFFFFF "+lng);
        
        if(lat== 0 && lng==0){
        	lat = 48.8534100;
    		lng = 2.3488000;
        }
        
        
		
		POINT_1 = new LatLng(lat, lng);
		
		addresses = new ArrayList<Address>();
		gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
		
		try {
			addresses = gcd.getFromLocation(lat, lng, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();
	}

	public void setValueForMap() {
		/*Log.i("aaaaaa","  "+lat);
		Log.i("dsfdffdf","  "+lng);*/
		//Log.i("aaaaaa","  "+addresses.get(0).getLocality());
		
		if (gMap != null) {
			MarkerOptions marker = new MarkerOptions().position(POINT_1).title(addresses.get(0).getLocality());
			// Changing marker icon
			marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.mk));
			point_1 = gMap.addMarker(marker);
		}
		
		CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(POINT_1, 10);	
		gMap.animateCamera(cameraUpdate);
	}
	
	/*
     * ****************************  header **********************************
     */
    public void header() {
		ImageView btn_setting = (ImageView) findViewById(R.id.image_setting);
		btn_setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent goToNextActivity = new Intent(getApplicationContext(),
						Settings.class);
				startActivity(goToNextActivity);
			}
		});

		/**
		 * Kiem user da dang nhap hay chua 1. hein thi icon user 2. hien thi
		 * icon add victimes
		 */
		ImageView image_open_left_navigation = (ImageView) findViewById(R.id.image_open_left_navigation);
		ImageView icon_header_form = (ImageView) findViewById(R.id.icon_header_form);
		if (MainActivity.userId.equalsIgnoreCase("0")) {
			image_open_left_navigation.setVisibility(View.VISIBLE);
			icon_header_form.setVisibility(View.GONE);
		} else {
			image_open_left_navigation.setVisibility(View.GONE);
			icon_header_form.setVisibility(View.VISIBLE);
		}

		image_open_left_navigation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent goToNextActivity = new Intent(getApplicationContext(),
						MainActivity.class);
				startActivity(goToNextActivity);
			}
		});

		icon_header_form.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent goToNextActivity = new Intent(getApplicationContext(),
						FormFirst.class);
				startActivity(goToNextActivity);
			}
		});
	}
}